package jman.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jman.mode.Vote;

public class VoteDao {
	public boolean addVote(Vote vote){
		boolean flag=false;
		Dao d=new Dao();
		d.start();
		String sql="insert into vote (raise_userid,status,result,title) values ("+vote.getRaise_UserId()+",'"+vote.getStatus()+"','"+vote.getResult()+"','"+vote.getTitle()+"')";
		flag=d.add(sql);
		d.close();
		return flag;
	}
	public int getId(Vote vote) throws SQLException{
		int id=0;
		Dao d=new Dao();
		d.start();
		String sql="select * from vote where raise_userid="+vote.getRaise_UserId()+" and title='"+vote.getTitle()+"' order by id desc limit 1";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			id=rs.getInt("id");
		}
		d.close();
		return id;
	}
	public List<Vote> getVoteListByStudentId(int id) throws SQLException{
		List<Vote> voteList=new ArrayList<Vote>();
		Dao d=new Dao();
		d.start();
		String sql="select * from vote where id in(select vote_id from vote_user2 where user_id="+id+" and status='是') order by create_time desc";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			Vote vote=new Vote(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
			voteList.add(vote);
		}
		d.close();
		return voteList;
	}
	public List<Vote> getVoteListByStudentIdToVote(int id) throws SQLException{
		List<Vote> voteList=new ArrayList<Vote>();
		Dao d=new Dao();
		d.start();
		String sql="select * from vote where id in(select vote_id from vote_user2 where user_id="+id+" and status='否')";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			Vote vote=new Vote(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
			voteList.add(vote);
		}
		d.close();
		return voteList;
	}
	public List<Vote> getVoteListByTeacherId(int id) throws SQLException{
		List<Vote> voteList=new ArrayList<Vote>();
		Dao d=new Dao();
		d.start();
		String sql="select * from vote where raise_userid="+id+" order by create_time desc";
		ResultSet rs=d.read(sql);
		while(rs.next()){
			Vote vote=new Vote(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6));
			voteList.add(vote);
		}
		d.close();
		return voteList;
	}
	public boolean update(int id,String name){
		boolean flag=false;
		Dao d=new Dao();
		d.start();
		String sql="update vote set status='完成',result='"+name+"' where id="+id;
		flag=d.update(sql);
		d.close();
		return flag;
	}
	public boolean delete(int id){
		boolean flag=false;
		Dao d=new Dao();
		d.start();
		String sql="delete from vote where id="+id;
		flag=d.delete(sql);
		d.close();
		return flag;
	}
}
